1 using UnityEngine;
2 using
System.Collections;
3
4 public
class IdleRunJump : MonoBehaviour
5 {
6     
protected Animator animator;
7     
public float DirectionDampTime = .25f;
8     
public bool ApplyGravity = true;
9     
public float SynchronizedMaxSpeed;
10     
public float TurnSpeedModifier;
11     
public float SynchronizedTurnSpeed;
12     
public float SynchronizedSpeedAcceleration;
13
14     
protected PhotonView m_PhotonView;
15
16     PhotonTransformView m_TransformView;
17
18     
//Vector3 m_LastPosition;
19     
float m_SpeedModifier;
20
21     
// Use this for initialization
22     
void Start ()
23     {
24         animator = GetComponent<Animator>();
25         m_PhotonView = GetComponent<PhotonView>();
26         m_TransformView = GetComponent<PhotonTransformView>();
27
28         
if(animator.layerCount >= 2)
29             animator.SetLayerWeight(
1, 1);
30     }
31         
32     
// Update is called once per frame
33     
void Update ()
34     {
35         
if( m_PhotonView.isMine == false && PhotonNetwork.connected == true )
36         {
37             
return;
38         }
39
40         
if (animator)
41         {
42             AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(
0);
43
44             
if (stateInfo.IsName("Base Layer.Run"))
45             {
46                 
if (Input.GetButton("Fire1")) animator.SetBool("Jump", true);
47             }
48             
else
49             {
50                 animator.SetBool(
"Jump", false);
51             }
52
53             
if(Input.GetButtonDown("Fire2") && animator.layerCount >= 2)
54             {
55                 animator.SetBool(
"Hi", !animator.GetBool("Hi"));
56             }
57             
58         
59             
float h = Input.GetAxis("Horizontal");
60             
float v = Input.GetAxis("Vertical");
61
62             
if( v < 0 )
63             {
64                 v =
0;
65             }
66
67             animator.SetFloat(
"Speed", h*h+v*v );
68             animator.SetFloat(
"Direction", h, DirectionDampTime, Time.deltaTime );
69
70             
float direction = animator.GetFloat( "Direction" );
71
72             
float targetSpeedModifier = Mathf.Abs( v );
73
74             
if( Mathf.Abs( direction ) > 0.2f )
75             {
76                 targetSpeedModifier = TurnSpeedModifier;
77             }
78
79             m_SpeedModifier = Mathf.MoveTowards( m_SpeedModifier, targetSpeedModifier, Time.deltaTime *
25f );
80
81             Vector3 speed = transform.forward * SynchronizedMaxSpeed * m_SpeedModifier;
82             
float turnSpeed = direction * SynchronizedTurnSpeed;
83
84             
/*float moveDistance = Vector3.Distance( transform.position, m_LastPosition ) / Time.deltaTime;
85
86             
if( moveDistance < 4f && turnSpeed == 0f )
87             {
88                 speed = transform.forward * moveDistance;
89             }*/

90
91             
//Debug.Log( moveDistance );
92             
//Debug.Log( speed + " - " + speed.magnitude + " - " + speedModifier + " - " + h + " - " + v );
93
94             m_TransformView.SetSynchronizedValues( speed, turnSpeed );
95
96             
//m_LastPosition = transform.position;
97          }
98     }
99 }


Vector3 m_LastPosition;

Use this for initialization

Update is called once per frame

Debug.Log( moveDistance );

Debug.Log( speed + " - " + speed.magnitude + " - " + speedModifier + " - " + h + " - " + v );

m_LastPosition = transform.position;




Trò chơi Tic-Tac-Toe, game đánh caro full source code 53.509 lượt xem

Gõ tìm kiếm nhanh...